package com.controller.admin;

import com.bean.bo.orders.ChangeStateBO;
import com.bean.bo.orders.ShowAllBO;
import com.bean.vo.Order.OrderVO;
import com.bean.vo.Order.UpdateOrderVO;
import com.bean.vo.admin.Result;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.service.admin.OrderServiceImplement;
import com.utils.GsonUtils;
import org.apache.commons.io.IOUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/api/admin/order/*")
public class OrderServlet extends HttpServlet {

    private static final Gson gson =GsonUtils.getGson();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace(request.getContextPath() + "/api/admin/order/", "");
        if ("deleteOrder".equals(action)){
            deleteOrder(request,response);
        }
        if ("ordersByPage".equals(action)){
            showAllOrder(request,response);
        }
        if ("order".equals(action)){
            showUpdateOrder(request,response);
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace(request.getContextPath() + "/api/admin/order/", "");
        if ("ordersByPage".equals(action)){
            showAllOrder(request,response);
        }
        if ("changeOrder".equals(action)){
            changeState(request,response);
        }
    }



    /**
     * 修改订单
     * @param request
     * @param response
     */
    private void changeState(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String requestString = IOUtils.toString(inputStream);
        ChangeStateBO changeStateBO = gson.fromJson(requestString, ChangeStateBO.class);
        OrderServiceImplement orderServiceImplement = new OrderServiceImplement();
        int updateState = orderServiceImplement.changeState(changeStateBO);
        if (updateState==200){
            response.getWriter().println(gson.toJson(new Result(0,null,null)));
            return;
        }
        response.getWriter().println(gson.toJson(new Result(10000,null,"修改失败")));

    }

    /**
     * 展示所有订单
     * @param request
     * @param response
     * @throws IOException
     */
    private void showAllOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String requestString = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        ShowAllBO showAllBO = gson.fromJson(requestString, ShowAllBO.class);
        OrderServiceImplement orderServiceImplement = new OrderServiceImplement();
        OrderVO orderVO = orderServiceImplement.showAllOrders(showAllBO);
        response.getWriter().println(gson.toJson(new Result(0,orderVO,null)));
    }

    /**
     * 修改订单页面回显信息
     * @param request
     * @param response
     */
    private void showUpdateOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String idString = request.getParameter("id");
        int id = Integer.parseInt(idString);
        OrderServiceImplement orderServiceImplement = new OrderServiceImplement();
        UpdateOrderVO data = orderServiceImplement.showUpdateOrder(id);
        String json = gson.toJson(new Result(0, data, null));
        System.out.println(json);
        response.getWriter().println(json);
    }


    /**
     * 删除订单
     * @param request
     * @param response
     */
    private void deleteOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idString = request.getParameter("id");
        Integer id=Integer.parseInt(idString);
        OrderServiceImplement orderServiceImplement = new OrderServiceImplement();
        Integer integer = orderServiceImplement.deleteOrder(id);
        if (integer==200){
            response.getWriter().println(gson.toJson(new Result(0,null,null)));
            return;
        }
        response.getWriter().println(gson.toJson(new Result(10000,null,"删除失败")));
    }

}
